package com.fastloan.mgt.invest.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.invest.dao.InvestTempMapper;
import com.fastloan.mgt.invest.dao.InvestTempOrgMapper;
import com.fastloan.mgt.invest.model.InvestTemp;
import com.fastloan.mgt.invest.model.InvestTempOrg;
import com.fastloan.mgt.invest.model.InvestType;
import com.fastloan.mgt.invest.model.vo.InvestSubjectChatVo;
import com.fastloan.mgt.invest.service.InvestTempService;
import com.fastloan.mgt.invest.service.InvestTypeService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.model.vo.OrgTreeVo;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author rankai
 *         createTime 2017-08-2017/8/8 10:33
 */
@Service
public class InvestTempServiceImpl extends BasicServiceImpl<InvestTemp> implements InvestTempService {

    @Autowired
    private InvestTempMapper investTempMapper;

    @Autowired
    private InvestTempOrgMapper investTempOrgMapper;

    @Autowired
    private CmuOrgService orgService;

    @Autowired
    private InvestTypeService investTypeService;

    @Override
    public List<InvestTemp> selectTempByManagerCode(Integer code) {
        return investTempMapper.selectTempByManagerCode(code);
    }

    @Override
    @Transactional
    public void release(InvestTemp investTemp, String orgIds) {
        if (investTemp.getState() == 3) {
            investTemp.setState(1);
            insertOrgs(orgIds, investTemp.getId());
        }
        if (investTemp.getState() == 1) {
            investTemp.setState(0);
        } else {
            investTemp.setState(1);
        }
        updateByPrimaryKeySelective(investTemp);
    }

    @Override
    public List<OrgTreeVo> selectForOrg(String tempId) {
        List<OrgTreeVo> orgTreeVos = orgService.selectSonByTop(ManagerKit.getTopOrgId(), true, true);
        InvestTempOrg investTempOrg = new InvestTempOrg();
        investTempOrg.setTempId(tempId);
        List<InvestTempOrg> investTempOrgs = investTempOrgMapper.select(investTempOrg);
        List<OrgTreeVo> selected = isSelected(orgTreeVos, investTempOrgs);
        return selected;
    }

    @Override
    public List<InvestTemp> selectByOrgId(String orgId) {
        return investTempMapper.selectByOrgId(orgId);
    }

    @Override
    @Transactional
    public void updateForOrg(InvestTemp investTemp, String orgIds) {
        if (!StringUtils.isBlank(orgIds)) {
            InvestTempOrg investTempOrg = new InvestTempOrg();
            investTempOrg.setTempId(investTemp.getId());
            investTempOrgMapper.delete(investTempOrg);
            insertOrgs(orgIds, investTemp.getId());
        }
        updateByPrimaryKeySelective(investTemp);
    }

    @Override
    public List<InvestSubjectChatVo> selectTempByManagerId() {
        return investTempMapper.selectTempByManagerId(ManagerKit.getUserId());
    }

    @Override
    public Map<String, Object> selectForAdd(String investTempId) {
        Map<String, Object> map = new HashMap<>();
        InvestTemp investTemp = selectByPrimaryKey(investTempId);
        map.put("investTemp", investTemp);
        String typeIds = investTemp.getTypeIds();
        if (!"0".equals(typeIds)) {
            String[] split = typeIds.split(",");
            List<String> list = Arrays.asList(split);
            Example example = new Example(InvestType.class);
            example.createCriteria().andIn("id", list);
            List<InvestType> investTypes = investTypeService.selectByExample(example);
            map.put("investType", investTypes);
        }
        return map;
    }

    @Override
    public List<InvestSubjectChatVo> selectSubjectByManagerId() {
        return investTempMapper.selectSubjectByManagerId(ManagerKit.getUserId().toString());
    }

    private void insertOrgs(String orgIds, String tempId) {
        String[] split = orgIds.split(",");
        List<InvestTempOrg> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            InvestTempOrg investTempOrgOne = new InvestTempOrg();
            investTempOrgOne.setOrgId(split[i]);
            investTempOrgOne.setTempId(tempId);
            list.add(investTempOrgOne);
        }
        investTempOrgMapper.insertList(list);
    }

    private List<OrgTreeVo> isSelected(List<OrgTreeVo> orgTreeVos, List<InvestTempOrg> investTempOrgs) {

        for (OrgTreeVo orgTreeVo : orgTreeVos) {
            for (InvestTempOrg investTempOrg : investTempOrgs) {
                if (investTempOrg.getOrgId().equals(orgTreeVo.getId())) {
                    orgTreeVo.setIsCheck("1");
                    break;
                }
            }
            List<OrgTreeVo> organizationList = orgTreeVo.getOrgTreeVos();
            if (organizationList != null && organizationList.size() > 0) {
                isSelected(organizationList, investTempOrgs);
            }
        }
        return orgTreeVos;
    }
}
